<?php
/**
 * Created by PhpStorm.
 * User: 独行侠
 * Date: 2018/3/6
 * Time: 17:48
 */

namespace App\Services\WorkOrder;


use App\Repositories\Order\OrderInfoOvertimeRepository;
use App\Repositories\Workorder\WorkorderRepository;
use App\Services\Category\CategoryService;
use App\Services\Orders\RefundDetailService;

class WorkOrderService
{

    /**
     * @var WorkorderRepository
     */
    protected $workorderRepository;

    protected $orderInfoOvertimeRepository;

    protected $categoryService;

    protected $refundDetailService;

    //生鲜售后期限时间(24小时)
    protected $fresh_time = 86400;

    //生鲜售后期限时间（7天）
    protected $other_time = 604800;

    //生鲜分类
    protected $fresh_cat = array(154,700,192,703,461);

    /**
     * WorkOrderService constructor.
     * @param WorkorderRepository $workorderRepository
     * @param CategoryService $categoryService
     * @param RefundDetailService $refundDetailService
     * @param OrderInfoOvertimeRepository $orderInfoOvertimeRepository
     */
    public function __construct(WorkorderRepository $workorderRepository,
                                CategoryService $categoryService,
                                RefundDetailService $refundDetailService,
                                OrderInfoOvertimeRepository $orderInfoOvertimeRepository)
    {

        $this->workorderRepository = $workorderRepository;
        $this->categoryService =$categoryService;
        $this->refundDetailService = $refundDetailService;
        $this->orderInfoOvertimeRepository = $orderInfoOvertimeRepository;

    }

    /**
     * 获取工单订单列表
     * @author Min
     * @param int $user_id
     * @param string $keyword
     * @param int $page_size
     * @return mixed
     */
    public function getOrderList($user_id=5960, $keyword='', $page_size=1){
        $order_list = $this->workorderRepository->getWorkOrderList($user_id, $keyword, $page_size)->toArray();

        //循环获取商品
        foreach ($order_list['data'] as $key=>$value) {
            $type = $this->checkOrderOvertime($value->order_id);
            $goods = $this->disposeGoods($value->order_id, $value->confirm_time, 1, '', $type);
            $value->order_date = local_date('Y-m-d H:i', $value->add_time);
            $value->order_time = local_to_time($value->add_time);
            $value->goods_list = $goods['goods_list'];
            $value->order_workorder_type = $type ? $type : $goods['order_workorder_type'];

        }

        return $order_list;
    }

    /**
     * 获取工单订单商品列表
     * @author Min
     * @return mixed
     */
    public function getOrderGoodsList($user_id, $order_id)
    {
        return $user_id;
    }

    /**
     * 查询是否有已处理的超时免单
     * @author Min
     * @param $order_id
     * @return int
     */
    protected function checkOrderOvertime($order_id){
        $where = [
            'order_id' => $order_id,
            'state' => 2
        ];

        $overTime = $this->orderInfoOvertimeRepository->findWhere($where)->toArray();

        return $overTime ? 4 : 0;
    }

    protected function disposeGoods($order_id=0, $confirm_time=0, $is_order_list=0, $type=0)
    {
        $in_overtime = 0;   //超过售后期限
        $in_time = 0;       //在售后期限
        $in_workorder = 0;  //已申请售后

        //获取工单订单商品列表
        $goods_list = $this->workorderRepository->getOrderGoodsList($order_id);

        foreach ($goods_list as $key=>$value) {
            $workorder_info = $this->workorderRepository->getWorkorderInfo(0, $order_id, $value['goods_id']);

            $goods_list[$key]['workorder_id'] = $workorder_info['id'] ? $workorder_info['id'] : 0;

            //是否在售后期限 1：是 ， 0：否
            $is_deadline = $this->getCategoryDeadline($value['cat_id'], $confirm_time);

            //工单类型 0：超过售后期限，1：在售后期限（可申请工单），2：已申请售后（查看详情）
            $goods_list[$key]['workorder_type'] = $workorder_info ? 2 : $is_deadline;

            if ($workorder_info) {
                $in_workorder = 1;
            } else {
                //订单商品是否已退款
                if ($this->refundDetailService->checkRefund($order_id, $value['goods_id'], $value['goods_number'])){
                    //商品已退款
                    $goods_list[$key]['workorder_type'] = 3;
                }

                if ($is_deadline)
                    $in_time = 1;
                else
                    $in_overtime = 1;
            }
            unset($goods_list[$key]['cat_id']);

            if ($type) {
                $goods_list[$key]['workorder_type'] = $type;
            }
        }

        if ($is_order_list == 0) {
            return $goods_list;
        }

        $data = [
            'goods_list' => $goods_list,
            'order_workorder_type' => $this->getOrderWorkorderType($in_workorder, $in_time, $in_overtime)
        ];

        return $data;
    }

    /**
     * 获取分类售后期限
     * @author Min
     * @param int $cat_id 商品分类id
     * @param int $confirm_time 订单确认时间
     * @return mixed
     */
    protected function getCategoryDeadline($cat_id, $confirm_time){
        //获取1级分类id
        $parent_cat_id = $this->categoryService->findLevelCatId($cat_id, 1);

        //是否在生鲜类
        if(in_array($parent_cat_id, $this->fresh_cat)){
            //售后期限
            $deadline = $confirm_time + $this->fresh_time;
        }else{
            //售后期限
            $deadline = $confirm_time + $this->other_time;
        }

        //是否在售后期限 1：是 ， 0：否
        $is_deadline = $deadline > gm_time() ? 1 : 0;

        return $is_deadline;
    }

    /**
     * @author Min
     * @param int $in_workorder 超过售后期限
     * @param int $in_time 在售后期限
     * @param int $in_overtime 已申请售后
     * @return int (0、全部过期,1、可申请,2、部分申请,3、全部申请)
     */
    protected function getOrderWorkorderType($in_workorder=0, $in_time=0, $in_overtime=0){
        //可申请
        $order_workorder_type = 1;
        //全部过期
        if ($in_workorder == 0 && $in_time == 0 && $in_overtime == 1) {
            $order_workorder_type = 0;
        }
        //全部申请
        if ($in_workorder == 1 && $in_time == 0 && $in_overtime == 0) {
            $order_workorder_type = 3;
        }
        //全部申请
        if ($in_workorder == 1 && $in_time == 1 && $in_overtime == 0) {
            $order_workorder_type = 2;
        }

        return $order_workorder_type;
    }

}